package com.dingqb.ktdemo.async

import java.util.*
import javax.script.ScriptEngineManager

class AsyncDemo:Foo{
    // 在后台线程池中运行该代码
    fun asyncOverlay() {
        val seq = sequence {
            for (i in 1..5) {
                // 产生一个 i 的平方
                yield(i * i * i)
            }
            // 产生一个区间
            yieldAll(-10..10)
        }

// 输出该序列
        println(seq.toList())
    }

    fun assr() {
        val numberRegex: Regex = "\\d[0-2]+".toRegex()
        val numbers = listOf("abc", "123", "456", "1221").filter(numberRegex::matches)
        for (ee in numbers) {
            println(ee);
        }

    }

    fun min() = minOf(11, 22)
    fun max() = maxOf(33, 22)

    fun java8() {
//        val engine = ScriptEngineManager().getEngineByExtension("kts")!!
//        engine.eval("val x = 3")
//        println(engine.eval("x + 2"))  // 输出 5
    }

    fun ex() {

    }
    fun unit():Unit{
        var s=Unit.toString();
        println(s);
        return Unit
    }


}

fun main() {
    var asyncDemo = AsyncDemo();
//    asyncDemo.asyncOverlay()
    asyncDemo.assr();
    println(asyncDemo.min())
    println(asyncDemo.max())
    asyncDemo.java8();
    fun foo(s: String?): Int {
        if (s != null) return s.length // 编译器自动将“s”转换为“String”
        return 0;
    }

    var len = foo("12345");
    println(len)
    println(Foo.answer)
    Foo.sayHello()
    println("main:"+asyncDemo.unit());
}

interface Foo {
    companion object {
        //@JvmField
        val answer: Int = 42

        //@JvmStatic
        fun sayHello() {
            println("Hello, world!")
        }
    }
}
